/*
 * Copyright (c) 2016- 2020 qolome.com Inc. All Rights Reserved.
 * ProjectName: qolome-knowledge
 * FileName: ConitanerSingleton.java
 * Author: jiangfeng@qolome.com
 * Date: 2020/2/28 下午1:03
 * LastModified: 2020/02/28 13:03:28
 */

package com.qolome.knowledge.design.pattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Qolome
 * @version V1.0
 * @Description 容器式单例
 * @date 2020年 02月28日 13:03:04
 */
public class ContainerSingleton {

    private ContainerSingleton(){}

    private  static final Map<String,Object> ioc = new ConcurrentHashMap<>();




    public static  Object getInstance(String className){
        Object instance = null;
            synchronized(ioc){
                if(!ioc.containsKey(className)){
                    try {
                        instance =  Class.forName(className).newInstance();
                        ioc.putIfAbsent(className,instance);
                    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    return instance;
                }else{
                    return ioc.get(className);
                }
        }
    }


}
